home *** CD-ROM | disk | FTP | other *** search
- /*
- ** Apple Macintosh Developer Technical Support
- **
- ** Program: MacShell
- ** File: menu.c
- ** Written by: Eric Soldan
- **
- ** Copyright © 1990-1991 Apple Computer, Inc.
- ** All rights reserved.
- */
-
-
-
- /*****************************************************************************/
-
-
-
- #include "MacShell.h" /* Get the MacShell includes/typedefs, etc. */
- #include "MacShellCommon.h" /* Get the stuff in common with rez. */
- #include "MacShell.protos" /* Get the prototypes for MacShell. */
-
- #ifndef __DESK__
- #include <Desk.h>
- #endif
-
- #ifndef __ERRORS__
- #include <Errors.h>
- #endif
-
- #ifndef __LISTCONTROL__
- #include "ListControl.h"
- #endif
-
- #ifndef __MEMORY__
- #include <Memory.h>
- #endif
-
- #ifndef __MENUS__
- #include <Menus.h>
- #endif
-
- #ifndef __TEXTEDITCONTROL__
- #include "TextEditControl.h"
- #endif
-
- #ifndef __TOOLUTILS__
- #include <ToolUtils.h>
- #endif
-
- #ifdef THINK_C
- #include "Utilities.h"
- #else
- #ifndef __UTILITIES__
- #include <Utilities.h>
- #endif
- #endif
-
-
-
- /*****************************************************************************/
-
-
-
- extern Boolean gQuitApplication;
- extern Boolean gHasAppleEvents;
-
-
-
- /*****************************************************************************/
- /*****************************************************************************/
-
-
-
- /* Enable and disable menus based on the current state. The user can only
- ** select enabled menu items. We set up all the menu items before calling
- ** MenuSelect or MenuKey, since these are the only times that a menu item can
- ** be selected. Note that MenuSelect is also the only time the user will see
- ** menu items. This approach to deciding what enable/ disable state a menu
- ** item has the advantage of concentrating all the decision-making in one
- ** routine, as opposed to being spread throughout the application. Other
- ** application designs may take a different approach that is just as valid.
- */
-
- #pragma segment Menu
- void AdjustMenus(void)
- {
- WindowPtr window;
- MenuHandle menu;
- short numWindows;
- Boolean maxWindows, menuEnabled, redrawMenuBar;
- static Boolean editMenuEnabled = true;
- static Boolean communicateMenuEnabled = true;
- #if MACSHELL_VERSION
- FileRecHndl frHndl;
- #endif
-
- redrawMenuBar = false;
- window = FrontWindow();
- menu = GetMHandle(mFile);
-
- EnableItem(menu, iNew); /* Set these for the no-windows state. */
- EnableItem(menu, iOpen);
- if (MaxBlock() < 0x10000L) {
- if (CompactMem(0x10000L) < 0x10000L) {
- DisableItem(menu, iNew); /* Running low on RAM. */
- DisableItem(menu, iOpen);
- }
- }
-
- DisableItem(menu, iClose);
- DisableItem(menu, iSave);
- DisableItem(menu, iSaveAs);
- DisableItem(menu, iDuplicate);
- DisableItem(menu, iPageSetup);
- DisableItem(menu, iPrint);
- /* Quit is always hilited. */
-
-
-
- if (IsDAWindow(window)) {
- DisableItem(menu, iNew); /* DAs don't get to do a new. */
- DisableItem(menu, iOpen); /* DAs don't get to do an open. */
- EnableItem(menu, iClose); /* Let DAs do a close from the menu. */
- }
-
- if (IsAppWindow(window)) {
- numWindows = GetWindowCount(false, false);
- maxWindows = (numWindows < kMaxNumWindows);
- EnableOrDisableItem(menu, iNew, maxWindows);
- EnableOrDisableItem(menu, iOpen, maxWindows);
- /* Allow a new and open only if max number of files not reached. */
-
- EnableItem(menu, iClose);
- EnableOrDisableItem(menu, iSave, GetWindowDirty(window));
- EnableItem(menu, iSaveAs);
- EnableItem(menu, iDuplicate);
-
- EnableItem(menu, iPageSetup);
- EnableItem(menu, iPrint);
- }
-
-
-
- menu = GetMHandle(mEdit);
- menuEnabled = false;
- if (IsDAWindow(window)) { /* A desk accessory may need edit menu. */
- menuEnabled = true;
- EnableItem(menu, iUndo);
- EnableItem(menu, iCut);
- EnableItem(menu, iCopy);
- EnableItem(menu, iPaste);
- EnableItem(menu, iClear);
- }
- #if MACSHELL_VERSION
- else CTEEditMenu(&menuEnabled, mEdit, iUndo, iCut);
- #endif
- if (editMenuEnabled != menuEnabled) {
- redrawMenuBar = true;
- if (editMenuEnabled = menuEnabled) EnableItem(menu, 0);
- else DisableItem(menu, 0);
- }
-
-
- #if MACSHELL_VERSION
-
- menu = GetMHandle(mCommunicate); /* Start by disabling everything. */
- menuEnabled = false;
- CheckItem(menu, iConnectToUser, false);
- DisableItem(menu, iConnectToUser);
- DisableItem(menu, iSendToUser);
-
- if (IsAppWindow(window)) {
-
- frHndl = (FileRecHndl)GetWRefCon(window);
-
- if (!(*frHndl)->fileState.readOnly) {
-
- if (gHasAppleEvents) {
- EnableItem(menu, iConnectToUser);
- menuEnabled = true;
- if ((*frHndl)->connect.connected) {
- DisableItem(menu, iConnectToUser);
- EnableItem(menu, iSendToUser);
- }
- CheckItem(menu, iConnectToUser, (*frHndl)->connect.connected);
- }
- }
- }
- if (communicateMenuEnabled != menuEnabled) {
- redrawMenuBar = true;
- if (communicateMenuEnabled = menuEnabled)
- EnableItem(menu, 0);
- else DisableItem(menu, 0);
- }
-
- #endif
-
- if (redrawMenuBar) DrawMenuBar();
- }
-
-
-
- /*****************************************************************************/
-
-
-
- /* This function either enables or disables a menu item. */
-
- #pragma segment Menu
- void EnableOrDisableItem(MenuHandle menu, short item, Boolean enable)
- {
- if (enable) EnableItem(menu, item);
- else DisableItem(menu, item);
- }
-
-
-
- /*****************************************************************************/
-
-
-
- /* This is called when an item is chosen from the menu bar (after calling
- ** MenuSelect or MenuKey). It performs the right operation for each command.
- ** It is good to have both the result of MenuSelect and MenuKey go to one
- ** routine like this to keep everything organized.
- */
-
- #pragma segment Menu
- void DoMenuCommand(long menuResult)
- {
- short menuID; /* The resource ID of the selected menu. */
- short menuItem; /* The item number of the selected menu. */
- Str255 daName;
- short daRefNum;
- FileRecHndl frHndl, newFrHndl;
- OSErr err;
- WindowPtr window;
-
- if (window = FrontWindow())
- frHndl = (FileRecHndl)GetWRefCon(window);
- /* frHndl is valid only if it is one of our windows. */
-
- menuID = HiWord(menuResult); /* Use macros for efficiency to get */
- menuItem = LoWord(menuResult); /* menu item number and menu number. */
-
- switch (menuID) {
-
- case mApple:
- switch (menuItem) {
- case iAbout: /* Bring up alert for About. */
- Alert(rAboutAlert, (ModalFilterProcPtr)keyEquivFilter);
- break;
- default: /* All non-About items in this menu are DAs. */
- GetItem(GetMHandle(mApple), menuItem, daName);
- daRefNum = OpenDeskAcc(daName);
- break;
- }
- break;
-
- case mFile:
- switch (menuItem) {
- case iNew:
- err = AppNewDocument(&frHndl, docFileType);
- if (!err) {
- if (err = DoNewWindow(frHndl, nil, (WindowPtr)-1, kwAppWindow))
- AppDisposeDocument(frHndl);
- }
- if (err) Alert(rErrorAlert, (ModalFilterProcPtr)alertFilter);
- break;
- case iOpen:
- err = AppOpenDocument(&frHndl, nil, fsRdWrPerm);
- if (!err) {
- if (err = DoNewWindow(frHndl, nil, (WindowPtr)-1, kwAppWindow))
- AppDisposeDocument(frHndl);
- }
- if ((err) && (err != userCanceledErr))
- Alert(rErrorAlert, (ModalFilterProcPtr)alertFilter);
- break;
- case iClose:
- DisposeOneWindow(window, iClose);
- break;
- case iSave:
- err = AppSaveDocument(frHndl, window, iSave);
- if ((err) && (err != userCanceledErr))
- Alert(rErrorAlert, (ModalFilterProcPtr)alertFilter);
- break;
- case iSaveAs:
- err = AppSaveDocument(frHndl, window, iSaveAs);
- if ((err) && (err != userCanceledErr))
- Alert(rErrorAlert, (ModalFilterProcPtr)alertFilter);
- break;
- case iDuplicate:
- err = AppDuplicateDocument(frHndl, &newFrHndl);
- if (!err) {
- if (err = DoNewWindow(newFrHndl, nil, (WindowPtr)-1, kwAppWindow))
- AppDisposeDocument(newFrHndl);
- }
- if (err) Alert(rErrorAlert, (ModalFilterProcPtr)alertFilter);
- break;
- case iPageSetup:
- DoSetCursor(&qd.arrow);
- PresentStyleDialog(frHndl);
- break;
- case iPrint:
- DoSetCursor(&qd.arrow);
- err = noErr;
- if (!(*frHndl)->doc.printRecValid)
- err = PresentStyleDialog(frHndl);
- if (!err) {
- err = AppPrintDocument(frHndl, true, true);
- AppPrintDocument(nil, false, false);
- }
- if ((err) && (err != userCanceledErr))
- Alert(rErrorAlert, (ModalFilterProcPtr)alertFilter);
- break;
- case iQuit:
- gQuitApplication = DisposeAllWindows();
- break;
- }
- break;
-
- case mEdit: /* Call SystemEdit for DA editing & MultiFinder. */
- if (IsAppWindow(window)) {
- #if MACSHELL_VERSION
- switch (menuItem) {
- case iUndo:
- case iCut:
- case iCopy:
- case iPaste:
- case iClear:
- BeginContent(window);
- if (menuItem == iUndo)
- CTEUndo();
- else
- CTEClipboard(menuItem - iCut + 2);
- EndContent(window);
- break;
- }
- if (menuItem != iCopy) (*frHndl)->fileState.docDirty = true;
- #endif
- }
- else SystemEdit(menuItem - 1);
- break;
-
- #if MACSHELL_VERSION
- case mCommunicate:
- switch (menuItem) {
- case iConnectToUser:
- DoSetCursor(&qd.arrow);
- if ((*frHndl)->connect.connected) SendMessage(frHndl, kDisconnectMssg);
- else SendConnect(frHndl);
- break;
- case iSendToUser:
- SendMessage(frHndl, kTextMssg);
- break;
-
- }
- break;
- #endif
-
- }
-
- HiliteMenu(0); /* Unhighlight what MenuSelect (or MenuKey) hilited. */
- }
-
-
-
-